<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import "@wangeditor/editor/dist/css/style.css";
import { useDetail } from "@/views/complain/utils/hooks";
import { reactive, ref } from "vue";
import {
  addComplain,
  deleteOwnerComplain,
  getAdmin,
  getOwnerComplainList,
  getResidentialList,
  scoreComplain
} from "@/api/user";

defineOptions({
  name: "userComplainList"
});

const form = reactive({
  name: ""
});
const loading = ref(true);
const resetForm = () => {
  form.name.value = "";
};
const residentialList = ref([]);
const adminList = ref([]);
const residential = ref();
const dataList = ref([]);
const { toDetail } = useDetail();
const complainDialog = ref(false);
const complianInfo = ref({ isAnonymous: false });
const AddComplain = ref(false);
const drawer = ref(false);

//初始化数据
async function onSearch() {
  //获取小区列表
  getResidentialList().then(res => {
    residentialList.value = res.data;
  });
  getAdmin().then(res => {
    adminList.value = res.data;
  });
  loading.value = false;
}

const getComplain = () => {
  getOwnerComplainList({ name: form.name, residentialId: residential.value }).then(res => {
    dataList.value = res.data;
  });
};
const score = () => {
  scoreComplain(complianInfo.value).then(res => {
    onSearch();
  });
};
const add = () => {
  addComplain(complianInfo.value).then(res => {
    onSearch();
  });
};
const ondblclick = (row: any) => {
  complianInfo.value = row;
  drawer.value = true;
};
const deleteComplain = (data: number) => {
  deleteOwnerComplain(data).then(res => {
    getComplain();
  });
};
onSearch();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="投诉内容：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入投诉内容"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="getComplain"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm()">
          重置
        </el-button>
        <el-button
          type="success"
          @click="AddComplain = true"
        >发布投诉
        </el-button>
      </el-form-item>
      <br />
      <el-form-item label="归属小区">
        <el-radio-group
          v-model="residential"
          size="small"
          @change="getComplain"
        >
          <el-radio-button value="-1" label="-1">
            社区
          </el-radio-button>
          <el-radio-button
            v-for="list in residentialList"
            :label="list.id"
            :value="list.id"
          >{{ list.name }}
          </el-radio-button
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-table :data="dataList.records" table-layout="fixed" @cell-dblclick="ondblclick">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="content" label="投诉内容" />
      <el-table-column label="回复内容">
        <template #default="scope">
          <span @dblclick="() => ondblclick(scope.row)">{{ scope.row.reply }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="created" label="投诉时间" />
      <el-table-column prop="operatorId" label="处理人员" />
      <el-table-column prop="updated" label="处理时间" />
      <el-table-column prop="score" label="评分" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="primary"
            size="small"
            v-if="scope.row.operatorId===null"
            @click="deleteComplain(scope.row.id)"
          >删除
          </el-button>
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.score===null"
            @click="complainDialog=true;complianInfo = scope.row"
          >评分
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%">
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="dataList.total"
        :page-count="dataList.current"
      />
    </div>
    <!--    评分弹窗-->
    <div>
      <el-dialog v-model="complainDialog" title="投诉评价" width="500" center>
    <span>
      星级：<el-rate v-model="complianInfo.score"></el-rate>
    </span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="complainDialog = false">取消</el-button>
            <el-button type="primary" @click="score();complainDialog = false">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <!--    投诉弹窗-->
    <div>
      <el-dialog v-model="AddComplain" title="发布投诉" width="500" center>
        <el-form-item>
          投诉方：
          <el-select
            allow-create
            filterable
            v-model="complianInfo.residentialId"
            placeholder="选择接诉单位"
          >
            <el-option value="-1" label="社区">
              社区
            </el-option>
            <el-option
              v-for="item in residentialList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
              {{ item.name }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          接诉方：
          <el-select
            allow-create
            filterable
            v-model="complianInfo.adminUnitId"
            placeholder="选择接诉单位"
          >
            <el-option value="1" label="社区">
              社区
            </el-option>
            <el-option
              value="2"
              label=""
            >
              物业
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          是否匿名：
          <el-switch
            v-model="complianInfo.isAnonymous"
            inline-prompt
            active-text="是"
            inactive-text="否"
          />
        </el-form-item>
        <el-form-item>
          投诉内容：
          <el-input v-model="complianInfo.content" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }"></el-input>
        </el-form-item>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="AddComplain = false">取消</el-button>
            <el-button type="primary" @click="add();AddComplain = false">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div>
      <el-drawer
        v-model="drawer"
        title="回复内容"
      >
        <div v-html="complianInfo.reply"></div>
      </el-drawer>
    </div>
  </div>
</template>

<style scoped lang="scss">

</style>
